package com.chenjun.core.service.sys.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysGroup;
import com.chenjun.core.dao.generator.mapper.SysGroupMapper;
import com.chenjun.core.dao.module.SysGroupRelationMapper;
import com.chenjun.core.service.activiti.SynGroupToActivitiService;
import com.chenjun.core.service.sys.SysGroupService;

@Service
@Transactional
public class SysGroupServiceImpl implements SysGroupService {
	
	/*private static final Logger logger = Logger.getLogger(SysGroupServiceImpl.class);*/

	@Autowired
    private SysGroupMapper sysGroupMapper;
    @Autowired
    private SysGroupRelationMapper	sysGroupRelationMapper;
   /* @Autowired
    private SysGroupRoleMapper sysGroupRoleMapper;*/
    @Autowired
    private SynGroupToActivitiService synGroupService;
	
	@Override
	public SysGroup get(String id) {
		return sysGroupMapper.selectByPrimaryKey(id);
	}

	@Override
	public int create(SysGroup record) {
		return sysGroupMapper.insert(record);
	}

	@Override
	public int delete(String id) {
		// 同步到activiti中
		synGroupService.deleteGroup(id);
		return sysGroupMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int update(SysGroup record) {
		return sysGroupMapper.updateByPrimaryKey(record);
	}

	@Override
	public int createOrUpdate(SysGroup record) {
		record.setStatus(1);// 默认启用
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			String groupID = UUID.randomUUID().toString();
			record.setId(groupID);
			Date date = Calendar.getInstance().getTime();
			record.setCreateTime(date);
			rows = sysGroupMapper.insert(record);
			// 同步到activiti
			synGroupService.addGroup(record);
		} else {
			rows = sysGroupMapper.updateByPrimaryKeySelective(record);
		}
		/*try {
			GroupDto groupDto = (GroupDto) record;
			SysGroupRole gr = new SysGroupRole();
			// 临时处理下ID 问题
			gr.setId(UUID.randomUUID().toString());
			gr.setGroupId(groupDto.getId());
			gr.setRoleId(groupDto.getRoleId());
			sysGroupRoleMapper.insert(gr);
		} catch (Exception e) {
			logger.error("SysGroup 向下转型 GroupDto 失败 >>>" + e.toString());
		}*/
		
		return rows;
	}

	@Override
	public int doCountByCondition(DataGridModel param) {
		return sysGroupRelationMapper.countByCondition(param);
	}

	@Override
	public List<SysGroup> doListByCondition(DataGridModel param) {
		return sysGroupRelationMapper.selectByCondition(param);
	}

	@Override
	public Pagination doPagination(DataGridModel param) {
		Pagination vo = new Pagination();
		List<SysGroup> list = sysGroupRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysGroupRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}

	@Override
	public SysGroup getResource(String id) {
		return sysGroupMapper.selectByPrimaryKey(id);
	}

	@Override
	public SysGroup getTypeById(String id) {
		SysGroup dto = new SysGroup();
		SysGroup resources = sysGroupMapper.selectByPrimaryKey(id);
		if (resources != null) {
			BeanUtils.copyProperties(resources, dto);
		}
		return dto;
	}

}
